const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();

//显示详情信息
    router.get('/updatnleaseInfo.html',function (req,res,next) {
        db.query("SELECT *FROM leaseinfo, classc, userinfo WHERE leaseinfo.classCId = classc.classCId AND userinfo.userId = leaseinfo.userId and leaseinfo.leaseid ="+ +req.query.leaseid+"",function (err,data) {
            if(err)
            {
                res.status(500).send('<script>alert("查询出错了!");window.location.href="404.html";</script>').end();
            }else
            {
                if(data.length==0)
                {
                    res.status(100).send('<script>alert("占时没有数据!");window.location.href="leaseInfo.html";</script>').end();
                }else
                {
                    req.leaseinfo=data;

                    next();
                }
            }
        })

    });
//修改
    router.use('/updatnleaseInfo',function(req,res,next){

        console.log(req.body);
                    db.query("UPDATE `leaseinfo` SET  `leaseend`='"+req.body.leaseend+"' WHERE leaseid="+req.body.leaseid+"",function (err,data) {
                        if(err)
                        {
                            res.status(500).send('<script>alert("出错啦");</script>').end();
                        }else
                        {
                            res.status(400).send('<script>alert("修改成功");window.location.href="leaseInfo.html";</script>').end();

                        }
                    });
    })
    router.get('/updatnleaseInfo.html',function(req,res){
        res.render("admin/updatnleaseInfo.ejs",{leaseinfolist:req.leaseinfo});
    });
    return router;
}